package controllers;

import com.bj.util.PageList;
import models.Answer;
import models.Question;
import org.apache.commons.lang.StringUtils;
import play.modules.router.Any;
import play.mvc.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-8-22
 * Time: 下午8:59
 * To change this template use File | Settings | File Templates.
 */
public class QuestionController extends Controller {

    @Any("/question/")
    public static void index(int page, Long id, String name, Integer status) {

        String sql = "from models.Question where 1 = 1 ";
        List listParams = new ArrayList();

        if (id != null) {
            sql += " and id = ? ";
            listParams.add(id);
        }

        if (StringUtils.isNotEmpty(name)) {
            sql += " and name like ? ";
            listParams.add("%" + name + "%");
        }

        if (status != null) {
            sql += " and status = ? ";
            listParams.add(status);
        }

        List list = Question.find(sql + " order by id desc ", listParams.toArray()).fetch(page, 10);
        long count = Question.count(sql, listParams.toArray());
        List questionList = new PageList(list, page, 10, count);
        renderArgs.put("questionList", questionList);
        render("/question/list.html");
    }

    @Any("/question/updateQuestionGrade")
    public static void updateQuestionGrade(long questionid, Long grade) {
        Question question = Question.findById(questionid);
        question.setGrade(grade);
        question.save();
        renderText("true");
    }

    @Any("/question/updateQuestionAnswer")
    public static void updateQuestionAnswer(long questionid, long answerid) {
        Question question = Question.findById(questionid);
        question.setAnswerId(answerid);
        question.save();
        renderText("true");
    }

    @Any("/question/updateAnswerName")
    public static void updateAnswerName(long[] id, String[] name) {
        for (int i = 0; i < id.length; i++) {
            Answer answer = Answer.findById(id[i]);
            answer.setName(name[i]);
            answer.save();
        }
        renderText("true");
    }

    @Any("/question/update")
    public static void update(long id, String name, Integer status) {
        Question question = Question.findById(id);
        if (name != null) {
            question.name = name.trim();
        }
        if (status != null) {
            question.status = status;
        }
        question.save();
        renderText("true");
    }

    @Any("/question/save")
    public static void save(String name, int status) {
        Question question = new Question();
        question.name = name.trim();
        question.status = status;
        question.save();

        Answer answer1 = new Answer();
        answer1.setQuestionId(question.id);
        answer1.save();

        Answer answer2 = new Answer();
        answer2.setQuestionId(question.id);
        answer2.save();

        Answer answer3 = new Answer();
        answer3.setQuestionId(question.id);
        answer3.save();

        Answer answer4 = new Answer();
        answer4.setQuestionId(question.id);
        answer4.save();

        redirect("/question/");
    }

}
